//
//  CYHDrawNewVC.m
//  Cat
//
//  Created by dllo on 15/11/28.
//  Copyright (c) 2015年 dllo. All rights reserved.
//

//http://api.huapipi.com/painting/latest?deviceId=1826162&pnum=1&uid=1033268

#import "CYHDrawNewVC.h"
#import "CYHDrawNewCell.h"
#import "MyFlowLayout.h"
#import "CYHHotOtherVC.h"

@interface CYHDrawNewVC ()<UICollectionViewDataSource, UICollectionViewDelegate>

@property (nonatomic, strong) UICollectionView *NewCollectionView;
@property (nonatomic, strong) NSMutableArray *NewArray;
@property (nonatomic, assign) NSInteger pnum;
@property (nonatomic, copy) NSString *verStr;

@end

@implementation CYHDrawNewVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self createView];
    [self createData];
    
    // 下拉刷新
    MJRefreshGifHeader *header = [MJRefreshGifHeader headerWithRefreshingTarget:self refreshingAction:@selector(createData)];
    self.NewCollectionView.header = header;
    
    // 上拉刷新
    MJRefreshAutoGifFooter *footer = [MJRefreshAutoGifFooter footerWithRefreshingTarget:self refreshingAction:@selector(createData)];
    self.NewCollectionView.footer = footer;
    // 刷新
    self.pnum = 1;
}

#pragma mark - createView
- (void)createView {
    UICollectionViewFlowLayout *flowyou = [[UICollectionViewFlowLayout alloc] init];
//    flowyou.MyDelegate = self;
    flowyou.itemSize = CGSizeMake(VIEW_WIDTH / 2 - 10, VIEW_HEIGHT / 3);
    flowyou.minimumInteritemSpacing = 5;
    flowyou.minimumLineSpacing = 10;
    flowyou.sectionInset = UIEdgeInsetsMake(5, 5, 5, 5);
    
    self.NewCollectionView = [[UICollectionView alloc] initWithFrame:self.view.frame collectionViewLayout:flowyou];
    self.NewCollectionView.delegate = self;
    self.NewCollectionView.dataSource = self;
    [self.view addSubview:self.NewCollectionView];
    self.NewCollectionView.backgroundColor = [UIColor colorWithRed:245 / 255.0 green:245 / 255.0 blue:245 / 255.0 alpha:1];
    
    [self.NewCollectionView registerClass:[CYHDrawNewCell class] forCellWithReuseIdentifier:@"New"];
    
}

#pragma mark - createData
- (void)createData {
    if ([self.NewCollectionView.footer isRefreshing]) {
        self.pnum++;
        
        NSString *URL = [NSString stringWithFormat:@"http://api.huapipi.com/painting/latest?deviceId=1826162&pnum=%ld&uid=1033268&ver=%@", self.pnum, self.verStr];
        
        [ToolNetWorking ToolNetWorking:URL block:^(id result) {
            
            // 防止为空
            NSMutableArray *Arr = [NSMutableArray array];
            for (NSDictionary *dic in result[@"rows"]) {
                if (dic != [NSNull null]) {
                    [Arr addObject:dic];
                }
            }
            
            //
            NSMutableArray *tempArr = [NSMutableArray array];
                tempArr = [HotModel BaseModel:Arr];
                [self.NewArray addObjectsFromArray:tempArr];
            
            // 拼接网址刷新
            HotModel *tempModel = tempArr.lastObject;
            self.verStr = tempModel.ver;
            
            if (self.NewArray.count == self.pnum * 10) {
                [self.NewCollectionView reloadData];
                [self.NewCollectionView.footer endRefreshing];
            }
        }];
        
    } else {
        self.pnum = 1;
        [ToolNetWorking ToolNetWorking:@"http://api.huapipi.com/painting/latest?deviceId=1826162&pnum=1&uid=1033268" block:^(id result) {
            
            // 防止为空
            NSMutableArray *Arr = [NSMutableArray array];
            for (NSDictionary *dic in result[@"rows"]) {
                if (dic != [NSNull null]) {
                    [Arr addObject:dic];
                }
            }
            
            //
            self.NewArray = [NSMutableArray array];
            self.NewArray = [HotModel BaseModel:Arr];
            
            // 拼接网址刷新
            HotModel *tempModel = self.NewArray.lastObject;
            self.verStr = tempModel.ver;
            
            if (self.NewArray != nil) {
                [self.NewCollectionView reloadData];
                [self.NewCollectionView.header endRefreshing];
            }
        }];
    }
}

#pragma mark - collectionView delegate
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.NewArray.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    CYHDrawNewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"New" forIndexPath:indexPath];
    HotModel *model = self.NewArray[indexPath.item];
    cell.model = model;
    
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    CYHHotOtherVC *otherVC = [[CYHHotOtherVC alloc] init];
    HotModel *model = self.NewArray[indexPath.item];
    otherVC.Secid = model.secid;
    [self.navigationController pushViewController:otherVC animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    self.tabBarController.tabBar.hidden = YES;
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    self.tabBarController.tabBar.hidden = NO;
}

//- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
//    
//    return CGSizeMake(VIEW_WIDTH / 2 - 10, (arc4random() % 200) + 180);
//}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
